##########################################################################
###  Replication Files for "The Unintended Consequences of Arms Embargoes"
###  Main Document Calculations
##########################################################################

#  Load Libraries
library(tidyverse)
library(gmodels)

###  Table 1:  Weapon Systems Transferred to Embargoed States, by Category
##  Load Data
arms <- read.csv("data.csv", header=T)

##  Subset Main Data to Embargo Observations
arms %>%
  mutate(during = ifelse(embargo_all ==2, 1, 0)) %>%
  filter(during==1) -> embarg

embarg %>%
  group_by(Description) %>%
  summarize(num = n(),
            ord = sum(No..ordered, na.rm=T),
            del = sum(No..delivered, na.rm=T)) -> test

##  Writing CSV File - Commenting out so user doesn't accidentally overwrite
##  previous save
# write.csv(test, "nums_chart.csv")

###  Then just open in Excel and sort all columns by the "num" variable!
###  Note:  There were a bunch of weapon types that had only a single order.
###  We cut off the table for space considerations, but the code above outputs
###  the entire thing.


###  Table 2:  Number of Transfers, Before, During, and After Embargo
##  Load Data
arms <- read.csv("data.csv", header=T)
arms <- subset(arms, arms$ngf_dum==0)

##  Subset main data into before, during, and after embargo splits
arms %>%
  mutate(sr_ccode = (s_ccode * 1000) + r_ccode) -> arms

#  Before
arms %>%
  filter(b4_bar_all == 2) -> b4b

#  After
arms %>%
  filter(aft_bar_all == 2) -> afb

#  During
arms %>%
  filter(embargo_all == 2) -> dur

##  Table 2, Before
count <- tapply(b4b$ones, b4b$r_ccode, sum)
mean(count)
median(count)

#  Table 2, Before, Time Correction
temp_max <- tapply(b4b$Year.of.order, b4b$r_ccode, max)
temp_min <- tapply(b4b$Year.of.order, b4b$r_ccode, min)

data_b4 <- tibble(names(count), count, temp_max, temp_min)
names(data_b4)[1] <- "r_ccode"
data_b4$dur <- data_b4$temp_max - data_b4$temp_min
data_b4$dur[data_b4$dur==0] <- 1
data_b4$ratio <- data_b4$count / data_b4$dur
mean(data_b4$ratio)
median(data_b4$ratio)

##  Table 2, During
count_dur <- tapply(dur$ones, dur$r_ccode, sum)
mean(count_dur)
median(count_dur)

#  Table 2, During, Time Correction
temp_max <- tapply(dur$Year.of.order, dur$r_ccode, max)
temp_min <- tapply(dur$Year.of.order, dur$r_ccode, min)

data_dur <- tibble(names(count_dur), count_dur, temp_max, temp_min)
names(data_dur)[1] <- "r_ccode"
data_dur$dur <- data_dur$temp_max - data_dur$temp_min
data_dur$dur[data_dur$dur==0] <- 1
data_dur$ratio <- data_dur$count_dur / data_dur$dur
mean(data_dur$ratio)
median(data_dur$ratio)

##  Table 2, After
count_af <- tapply(afb$ones, afb$r_ccode, sum)
mean(count_af)
median(count_af)

#  Table 2, After, Time Correction
temp_max <- tapply(afb$Year.of.order, afb$r_ccode, max)
temp_min <- tapply(afb$Year.of.order, afb$r_ccode, min)

data_af <- tibble(names(count_af), count_af, temp_max, temp_min)
names(data_af)[1] <- "r_ccode"
data_af$dur <- data_af$temp_max - data_af$temp_min
data_af$dur[data_af$dur==0] <- 1
data_af$ratio <- data_af$count_af / data_af$dur
mean(data_af$ratio)
median(data_af$ratio)


###  Table 3:  Number of Individual Weapon Systems per Order
##  Load Data
arms <- read.csv("data.csv", header=T)
arms <- subset(arms, arms$ngf_dum==0)

##  Subset Main Data to Embargo and Non-Embargo Observations
#  Embargo:  "embarg"
arms %>%
  mutate(during = ifelse(embargo_all ==2, 1, 0)) %>%
  filter(during==1) -> embarg

#  Non-Embargo:  "nembar"
arms %>%
  mutate(during = ifelse(embargo_all ==2, 1, 0)) %>%
  filter(during==0) -> nembar

mean(embarg$No..ordered, na.rm=T)
mean(embarg$No..delivered, na.rm=T)
median(embarg$No..ordered, na.rm=T)
median(embarg$No..delivered, na.rm=T)

mean(nembar$No..ordered, na.rm=T)
mean(nembar$No..delivered, na.rm=T)
median(nembar$No..ordered, na.rm=T)
median(nembar$No..delivered, na.rm=T)


###  Time to Receive Weapons
##  Load Data
arms <- read.csv("data.csv", header=T)
arms <- subset(arms, arms$ngf_dum==0)

arms %>%
  mutate(during = ifelse(embargo_all ==2, 1, 0),
         dura = deliver_last - deliver_first) %>%
  filter(during==1) -> embarg

arms %>%
  mutate(during = ifelse(embargo_all ==2, 1, 0),
         dura = deliver_last - deliver_first) %>%
  filter(during==0) -> nembar

(mean(nembar$dura, na.rm=T) - mean(embarg$dura, na.rm=T)) * 365
#  62.58266 days


###  Table 4:  Cross-Tabulation of Direct vs. Middleman Transfers with Transfers of New vs. Secondhand/Refurbished Systems
##  Load Data
arms <- read.csv("data.csv", header=T)
arms <- subset(arms, arms$ngf_dum==0)

##  Subset Main Data to Embargo and Non-Embargo Observations
#  Embargo:  "embarg"
arms %>%
  mutate(during = ifelse(embargo_all ==2, 1, 0)) %>%
  filter(during==1) -> embarg

#  Non-Embargo:  "nembar"
arms %>%
  mutate(during = ifelse(embargo_all ==2, 1, 0)) %>%
  filter(during==0) -> nembar

#  Non-Embargoed (Top Sub-Table)
nembar$new <- ifelse(nembar$Status == "New", 1, 0)
CrossTable(nembar$middle, nembar$new)

#  Embargoed (Bottom Sub-Table)
embarg$new <- ifelse(embarg$Status == "New", 1, 0)
CrossTable(embarg$middle, embarg$new)


###  Table 5
##  Load Data
arms <- read.csv("data.csv", header=T)
arms <- subset(arms, arms$ngf_dum==0)

##  Subset main data into before, during, and after embargo splits
arms %>%
  mutate(sr_ccode = (s_ccode * 1000) + r_ccode) -> arms

#  Before
arms %>%
  filter(b4_bar_all == 2) -> b4b

#  After
arms %>%
  filter(aft_bar_all == 2) -> afb

#  During
arms %>%
  filter(embargo_all == 2) -> dur

##  Table 5, Before, Number of Unique Suppliers
count_b <- tapply(b4b$s_ccode, b4b$r_ccode, unique)
test <- lengths(count_b)
mean(test)
#  8.44444
median(test)
# 8

##  Table 5, Before, Number of Unique Suppliers, Time Corrected
nums <- tapply(b4b$ones, b4b$r_ccode, sum)

temp_max <- tapply(b4b$Year.of.order, b4b$r_ccode, max)
temp_min <- tapply(b4b$Year.of.order, b4b$r_ccode, min)

data_b4 <- tibble(names(lengths(test)), test, temp_max, temp_min, nums)
names(data_b4)[1:2] <- c("r_ccode", "count")
data_b4$dur <- data_b4$temp_max - data_b4$temp_min
data_b4$dur[data_b4$dur==0] <- 1
data_b4$ratio <- data_b4$count / data_b4$dur
mean(data_b4$ratio)
# [1] 0.3450232
median(data_b4$ratio)
# [1] 0.2886905

#   Table 5, Before, Transactions over number of suppliers, Time Corrected
mean(data_b4$nums/data_b4$count/data_b4$dur)
median(data_b4$nums/data_b4$count/data_b4$dur)

##  Table 5, During, Number of Unique Suppliers
count_d <- tapply(dur$s_ccode, dur$r_ccode, unique)
test <- lengths(count_d)
mean(test)
#  4.363636
median(test)
# 4

#  Table 5, During, Transactions over number of suppliers, Time Corrected
nums <- tapply(dur$ones, dur$r_ccode, sum)

temp_max <- tapply(dur$Year.of.order, dur$r_ccode, max)
temp_min <- tapply(dur$Year.of.order, dur$r_ccode, min)

data_dur <- tibble(names(test), test, temp_max, temp_min, nums)
names(data_dur)[1:2] <- c("r_ccode", "count")
data_dur$dur <- data_dur$temp_max - data_dur$temp_min
data_dur$dur[data_dur$dur==0] <- 1
data_dur$ratio <- data_dur$count / data_dur$dur
mean(data_dur$ratio)
# 0.8481849
median(data_dur$ratio)
# 0.8333333

#  Table 5, During, Transactions over number of suppliers, Time Corrected
mean(data_dur$nums/data_dur$count/data_dur$dur)
median(data_dur$nums/data_dur$count/data_dur$dur)

##  Table 5, After, Number of Unique Suppliers
count_a <- tapply(afb$s_ccode, afb$r_ccode, unique)
test <- lengths(count_a)
mean(test)
#  5.866667
median(test)
# 5

#  Table 5, After, Transactions over number of suppliers, Time Corrected
nums <- tapply(afb$ones, afb$r_ccode, sum)

temp_max <- tapply(afb$Year.of.order, afb$r_ccode, max)
temp_min <- tapply(afb$Year.of.order, afb$r_ccode, min)

data_af <- tibble(names(test), test, temp_max, temp_min, nums)
names(data_af)[1:2] <- c("r_ccode", "count")
data_af$dur <- data_af$temp_max - data_af$temp_min
data_af$dur[data_af$dur==0] <- 1
data_af$ratio <- data_af$count / data_af$dur
mean(data_af$ratio)
# 0.8269059
median(data_af$ratio)
# 0.8

#  Table 5, After, Transactions over number of suppliers, Time Corrected
mean(data_af$nums/data_af$count/data_af$dur)
median(data_af$nums/data_af$count/data_af$dur)


###  Table 6:  Top 10 Suppliers Selling Arms through an Embargo
#  Underlying data can be found in "Busters Jan 2022.xlsx"

###  Paragraph starting "But Iraq is an outlier.  Of the 36 countries embargoed, 28 remain with their "family" of national origin."
#  Underlying data can be found in "Diversification of Arms.xlsx"